﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace MainClient
{
    public partial class frmChef : Form
    {
        private Hashtable ShowInfo = null;
        private string chefID;
        private string dbitemid;

        public frmChef(string strChefID)
        {
            InitializeComponent();
            this.chefID = strChefID;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.lbl_curtime.Text = DateTime.Now.ToString();
        }

        private void chefForm_Load(object sender, EventArgs e)
        {
            timer1.Start();   // start timer to display current time
        }

        private void btn_chef_get_Click(object sender, EventArgs e)
        {
            DataAccess.CookDAO chefDAO = new DataAccess.CookDAO();
            ShowInfo = new Hashtable();
            lbl_error.Visible = false;

            // 判断是否已经进入“正在准备状态” - 厨师第二次点击
            if (btn_chef_get.Text.ToString().Trim() == "已完成")
            {
                if (!chefDAO.setComplete(this.chefID, this.dbitemid))
                {
                    lbl_error.Text = "提交失败，请稍候再试！";
                    lbl_error.Visible = true;
                    return;
                }

                btn_chef_get.Text = "准备做菜";
                lbl_error.Visible = false;

                this.lbl_foodID.Text = "";
                this.lbl_foodName.Text = "";
                this.lbl_notes.Text = "";
                return;
            }

            // 以厨师cookID申请菜
            ShowInfo = (Hashtable)chefDAO.doOneFood(this.chefID);
            if ((String)ShowInfo["id"] ==null)
            {
                lbl_error.Text = "现在没有需要做的菜，请稍候！";
                lbl_error.ForeColor = Color.Red;
                lbl_error.Visible = true;
            }
            else
            {
                this.dbitemid = ShowInfo["id"].ToString().Trim();
                this.lbl_foodID.Text = (String)ShowInfo["foodID"];
                this.lbl_foodName.Text = (String)ShowInfo["foodName"];
                this.lbl_notes.Text = (String)ShowInfo["notes"];

                this.btn_chef_get.Text = "已完成";
            }            
        }

        private void frmChef_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

    }
}
